package controller;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.annotation.ManagedBean;
import javax.faces.context.FacesContext;
import model.dao.ProjectDao;
import model.dao.SprintDao;
import model.domain.Project;
import model.domain.Sprint;

@ManagedBean
public class projectBean {

	private Project project;
        private Project selectedProject;
        private Project newProject;
        private Project editProject;
        private List<Project> projects;
        
	public projectBean(){
            this.project = new Project();
            this.selectedProject = new Project();
            this.editProject = new Project();

            ProjectDao home = new ProjectDao();
            Project aux = home.getFirst();
            if(aux != null){
                this.project.setProjectID(aux.getProjectID());
                this.project.setProjectName(aux.getProjectName());
                this.selectedProject.setProjectID(aux.getProjectID());
                this.selectedProject.setProjectName(aux.getProjectName());
                this.editProject.setProjectID(aux.getProjectID());
                this.editProject.setProjectName(aux.getProjectName());
            }
	}

        public void setProject(Project object){
            this.project = object;
        }

	public Project getProject(){
		return this.project;
	}

        public void setEditproject(Project object){
            this.editProject = object;
        }

	public Project getEditproject(){
		return this.editProject;
	}

        public void setSelectedproject(Project object){
            this.selectedProject = object;
        }

	public Project getSelectedproject(){
		return this.selectedProject;
	}

        public void setNewproject(Project object){
            this.newProject = object;
        }

	public Project getNewproject(){
            if(this.newProject == null){
                this.newProject = new Project();
            }
            return this.newProject;
	}

        public List<Project> getProjects(){

            ProjectDao home = new ProjectDao();
            this.projects = home.getAll();
            return this.projects;
        }

        public String submitSelected(){
            if(this.project == null){
                this.project = new Project();
            }
            this.project.setProjectName(this.selectedProject.getProjectName());
            this.project.setProjectID(this.selectedProject.getProjectID());
            this.editProject.setProjectName(this.selectedProject.getProjectName());
            this.editProject.setProjectID(this.selectedProject.getProjectID());
            return null;
        }

        public void submitCreate() throws IOException{

            ProjectDao home = new ProjectDao();

            Project auxProject = new Project();
            auxProject.setProjectName(this.newProject.getProjectName());

            home.insert(auxProject);

            this.selectedProject.setProjectID(auxProject.getProjectID());
            this.selectedProject.setProjectName(auxProject.getProjectName());
            this.project.setProjectID(auxProject.getProjectID());
            this.project.setProjectName(auxProject.getProjectName());
            this.editProject.setProjectID(auxProject.getProjectID());
            this.editProject.setProjectName(auxProject.getProjectName());
            this.newProject.setProjectName("");
            this.newProject.setProjectID(-1);

            FacesContext.getCurrentInstance().getExternalContext().redirect("Project.xhtml");
        }

        public void submitEdit() throws IOException{

            ProjectDao home = new ProjectDao();
            home.insertOrUpdate(this.editProject);

            this.selectedProject.setProjectID(this.editProject.getProjectID());
            this.selectedProject.setProjectName(this.editProject.getProjectName());
            this.project.setProjectID(this.editProject.getProjectID());
            this.project.setProjectName(this.editProject.getProjectName());

            FacesContext.getCurrentInstance().getExternalContext().redirect("Project.xhtml");
        }

        public void submitDelete() throws IOException{
            ProjectDao home = new ProjectDao();
            home.delete(this.project);

            Project aux = home.getFirst();
            if(aux != null){
                this.project.setProjectID(aux.getProjectID());
                this.project.setProjectName(aux.getProjectName());
                this.selectedProject.setProjectID(aux.getProjectID());
                this.selectedProject.setProjectName(aux.getProjectName());
                this.editProject.setProjectID(aux.getProjectID());
                this.editProject.setProjectName(aux.getProjectName());
            }

            FacesContext.getCurrentInstance().getExternalContext().redirect("Project.xhtml");
        }

        public Integer getNumberOfSprint(){
            SprintDao home = new SprintDao();
            return home.countSprints(this.project).intValue();
        }

        public Integer getDaysToFinishSprint() throws ParseException{
            Sprint currentSprint = getCurrentSprint();

            if(currentSprint == null)
                return 0;
            
            Calendar now = Calendar.getInstance();
            Long daysToFinishSprint = this.getDateDiffInDays(now.getTime(), currentSprint.getEnd()) + 1;
            return daysToFinishSprint.intValue();
        }

    public Sprint getCurrentSprint() throws ParseException{
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        Calendar cal = Calendar.getInstance();
        Date now = dateFormat.parse(dateFormat.format(cal.getTime()));
        SprintDao home = new SprintDao();
        Sprint currentSprint = home.findByDate(now, this.project);
        return currentSprint;
    }

    private Long getDateDiffInDays(Date start, Date end){
        Long dateDiffInMilliseconds = end.getTime() - start.getTime();
        return dateDiffInMilliseconds/ (24 * 60 * 60 * 1000);
    }
}
